package cm.mefobe.android.telecomcost.view;

import cm.mefobe.android.telecomcost.entities.Week;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WeekView extends LinearLayout {

	private Context _context;
	private Week _week;
	private TextView _weekNo;
	private TextView _dateRange;
	
	public WeekView(Context context,Week week) {
		super(context);
		Log.i("WeekView", "creating the WeekView + week");
		_context = context;
		setWeek(week);
	}
	
	public WeekView(Context context) {
		super(context);
		Log.i("WeekView", "creating the WeekView - week");
		_context = context;	
		setWeekView();
	}

	private void setWeekView() {
		if (isInEditMode()) {
			Log.i("WeekView", "isInEditMode");
		} else {
			if (_weekNo == null) {
				Log.i("WeekView", "adding views...");
				setOrientation(LinearLayout.VERTICAL);

				_weekNo = new TextView(_context);
				_dateRange = new TextView(_context);					
				_weekNo.setTextSize(20);				
								
				addView(_weekNo);
				addView(_dateRange);	
				
				if(_week != null)
				{
					_weekNo.setText(_week.getName());
					_dateRange.setText(_week.getDayRangeLabel());
				}
			} else {
				Log.i("WeekView", "WeekView already set...");				
			}
		}
	}

	public WeekView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("WeekView", "creating the WeekView with AttributeSet");

		_context = context;
		setWeekView();
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		Log.i("WeekView", "onLayout");
		super.onLayout(changed, l, t, r, b);
		setWeekView();
	}

	@Override
	protected void onAttachedToWindow() {
		Log.i("WeekView", "onAttachedToWindow");
		super.onAttachedToWindow();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("WeekView", "onDraw");
		super.onDraw(canvas);
	}

	@Override
	protected void onFinishInflate() {
		Log.i("WeekView", "onFinishInflate");
		super.onFinishInflate();
		setWeekView();
	}

	@Override
	public void updateViewLayout(View view,
			android.view.ViewGroup.LayoutParams params) {
		Log.i("WeekView", "updateViewLayout");
		super.updateViewLayout(view, params);
	}	

	public void setWeek(Week _week) {
		Log.i("WeekView", "setWeek........." + _week.toString());
		this._week = _week;
		if(_weekNo == null)
		{
			Log.i("WeekView", "_weekNo is null.......we setWeekView() first");
			setWeekView();			
		}
		
		_weekNo.setText(_week.getName());
		_dateRange.setText(_week.getDayRangeLabel());
	}

	public Week getWeek() {
		return _week;
	}
}